package com.dinadi.yidi.ui.fragment;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.SDesignerAdapter;
import com.dinadi.yidi.base.BaseFragment;
import com.dinadi.yidi.bean.MessageBean;
import com.dinadi.yidi.bean.TotalDesignerRequest;
import com.dinadi.yidi.bean.TotalDesignerRequestEntity;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * author: JiaYu.He
 * Date: 2017/5/15  搜索---设计师
 * Time: 16:04
 */
public class SDesignerFragment extends BaseFragment {
    private View view;
    private TextView  text_prompt;
    private PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;
    private SDesignerAdapter mRefreshAdapter;
    private List<TotalDesignerRequestEntity> dataList = new ArrayList<>();
    int page = 1;
    private String userId, token,searchContent;

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_s_designer, null);
        return view;
    }

    @Override
    protected void initView() {
        changePager(2);
        mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView) view.findViewById(R.id.pullLoadMoreRecyclerView);
        //显示下拉刷新
        // mPullLoadMoreRecyclerView.setRefreshing(true);
        mPullLoadMoreRecyclerView.setLinearLayout();
        mRefreshAdapter = new SDesignerAdapter(getActivity());
        mPullLoadMoreRecyclerView.setAdapter(mRefreshAdapter);
        mPullLoadMoreRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreListener());
        text_prompt = (TextView) view.findViewById(R.id.text_prompt);

        //注册EventBus  接收页面
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void clickEvent(View v) {

    }

    @Override
    protected void initData() {
        userId = SharePreferencesHelper.getInstance(getActivity()).getString("userId");
        token = SharePreferencesHelper.getInstance(getActivity()).getString("token");
        initPullrefresh();
    }

    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                TotalDesignerRequest bean = JsonUtil.parseJsonToBean(response, TotalDesignerRequest.class);
                dataList = bean.getDesignerList();
                mRefreshAdapter.addAllData(dataList);
                if (page == 1) {
                    if (dataList.size() == 0) {
                        text_prompt.setVisibility(View.VISIBLE);
                    } else {
                        text_prompt.setVisibility(View.GONE);

                    }
                } else {
                    text_prompt.setVisibility(View.GONE);

                }
                getData();
            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            closeLoadingDialog();
            toast("加载失败");
        }
    };

    private void getData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新结束
                mPullLoadMoreRecyclerView.setPullLoadMoreCompleted();
            }
        }, 1000);
    }

    class PullLoadMoreListener implements PullLoadMoreRecyclerView.PullLoadMoreListener {
        @Override
        public void onRefresh() {
            setRefresh();
        }

        @Override
        public void onLoadMore() {
            page = page + 1;
            initPullrefresh();
        }
    }

    private void setRefresh() {
        mRefreshAdapter.clearData();
        page = 1;
        initPullrefresh();
    }

    private void initPullrefresh() {
        showLoadingDialog(getResources().getString(R.string.dialog_text));
        MessageBean bean = new MessageBean();
        bean.setToken(token);
        bean.setUserId(userId);
        bean.setPageNum(page + "");
        bean.setPageSize("10");
        bean.setUserName(searchContent);
        bean.setType("2");
        CommonInterface messageInterface = new CommonInterface(getActivity(), httpListener);
        messageInterface.getInterface("13001", JSON.toJSONString(bean));
    }

    public void onEventMainThread(String message) {
        searchContent = message;
        dataList.clear();
        mRefreshAdapter.clearData();
        initPullrefresh();

    }

}
